<template>
  <!-- 我的页面 -->
  <div class='my-box'>
    <!-- 未登录 -->
    <div class='not-login header-bg' v-if='!user'>
      <img src="@/assets/mobile.png" @click='$router.push("/login")'>
      <span>登录/注册</span>
    </div>
    <!-- 已登录 -->
    <div class='login-not header-bg' v-else>
      <div class='user-info'>
        <div class='user-container'>
          <div class='left'>
            <van-image round width="1rem" height="1rem" :src="userInfo.photo" clas='avatar' />
            <span class='username'>{{userInfo.name}}</span>
          </div>
          <van-button type="default" size='mini' round to='/user'>编辑资料</van-button>
        </div>
      </div>
      <div class='data-info'>
        <div class='data-item'>
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class='data-item'>
          <span>{{userInfo.fans_count}}</span>
          <span>关注</span>
        </div>
        <div class='data-item'>
          <span>{{userInfo.follow_count}}</span>
          <span>粉丝</span>
        </div>
        <div class='data-item'>
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
      <!-- 宫格 -->
      <van-grid class='nav-grid' :column-num='2'>
        <van-grid-item>
          <i class='toutiao toutiao-shoucang'></i>
          <span class='grid-text'>收藏</span>
        </van-grid-item>
        <van-grid-item>
          <i class='toutiao toutiao-lishi'></i>
          <span class='grid-text'>历史</span>
        </van-grid-item>
      </van-grid>
      <div class='box-cell'>
        <van-cell title="消息通知" is-link />
        <van-cell title="小智同学" is-link />
      </div>
      <van-cell @click='logout' class='logout-cell' v-if='user' title="退出登录" />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserProfile } from '@/api/user.js'
export default {
  name: 'my',
  data () {
    return {
      // 用户个人数据
      userInfo: {}
    }
  },
  methods: {
    // 退出功能
    logout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出'
      })
        .then(() => {
          this.$store.commit('lintUser', null)
        })
        .catch(() => { })
    },
    // 获取用户个人数据
    async loadUser () {
      try {
        const res = await getUserProfile()
        this.userInfo = res.data.data
      } catch (err) {
        this.$toast('获取失败')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    // 获取用户个人数据
    if (this.user) {
      this.loadUser()
    }
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.my-box {
  .header-bg {
    width: 750px;
    height: 400px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      margin-top: 15px;
      color: #fff;
    }
  }
  .login-not {
    .user-info {
      padding: 0 32px;
      height: 247px;
      // background-color: red;
      display: flex;
      align-items: flex-end;
      .user-container {
        height: 132px;
        display: flex;
        justify-content: space-between;
        width: 100%;
        align-items: center;
      }
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
        }
        .username {
          font-size: 30px;
          color: #fff;
          margin-left: 22px;
        }
      }
    }
    .data-info {
      height: 153px;
      // background-color: #fff;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        span:nth-child(1) {
          font-size: 30px;
        }
        span:nth-child(2) {
          font-size: 22px;
          margin-top: 20px;
        }
      }
    }
    .nav-grid {
      .toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      .grid-text {
        font-size: 27px;
        color: #333;
        margin-top: 16px;
        letter-spacing: 10px;
      }
    }
    .box-cell {
      margin: 10px 0;
    }
    .logout-cell {
      color: #d86262;
      text-align: center;
    }
  }
}
</style>
